;
(function(window, document, $) {
    var Drag, Variable = {},
        _key, $this;
    var params = {
        left: 0,
        top: 0,
        currentX: 0,
        currentY: 0,
        flag: false
    };

    Drag = function(el, options) {
        var me = this;
        this._init(el, options);
    };

    Drag.prototype = {
        _init: function(el, options) {
            var me = this;
            me.options = options;

            me.options.left = 0;
            me.options.top = 0;
            me.options.currentX = 0;
            me.options.currentY = 0;
            me.options.flag = 0;

            me._event = {
                after: []
            };

            this._drag($(el)[0], me.options.target[0]);
        },
        on: function(event, callback) {
            var me = this;
            switch (event) {
                case 'after':
                    me._event.after.push(callback)
                    break;
            }
            return me;
        },
        _execEvent: function(event, e) {
            var me = this
            var i = me._event[event].length - 1;
            if (i >= 0) {
                return me._event[event][i].call(me, e);
            }
        },
        _drag: function(triggerObject, targetObejct, callback) {
            if (methods._css(targetObejct, "left") !== "auto") {
                //params.left = methods._css(targetObejct, "left");
                this.options.left = methods._css(targetObejct, "left");
            }
            if (methods._css(targetObejct, "top") !== "auto") {
                //params.top = methods._css(targetObejct, "top");
                this.options.top = methods._css(targetObejct, "top");
            }
            triggerObject.onmousedown = function(event) {
                _key = $(this).attr("drag-widget-key");
                Variable[_key].options.flag = true;

                if (!event) {
                    event = window.event;
                    //防止IE文字选中
                    Variable[_key].options.trigger[0].onselectstart = function() {
                        return false;
                    }
                }
                var e = event;
                Variable[_key].options.currentX = e.clientX;
                Variable[_key].options.currentY = e.clientY;
            };
            document.onmouseup = function() {
                if (_key) {
                    Variable[_key].options.flag = false;
                    if (methods._css(Variable[_key].options.target[0], "left") !== "auto") {
                        Variable[_key].options.left = methods._css(Variable[_key].options.target[0], "left");
                    }
                    if (methods._css(Variable[_key].options.target[0], "top") !== "auto") {
                        Variable[_key].options.top = methods._css(Variable[_key].options.target[0], "top");
                    }
                }

            };
            document.onmousemove = function(event) {
                var e = event ? event : window.event;
                if (_key && Variable[_key].options.flag) {
                    var nowX = e.clientX,
                        nowY = e.clientY;

                    var disX = nowX - Variable[_key].options.currentX,
                        disY = nowY - Variable[_key].options.currentY;

                    //if (disX > 0) {
                    //    if (parseInt(methods._css(Variable[_key].options.target[0], "left")) + $(Variable[_key].options.target).outerWidth() + 10 > $(window).width()) {
                    //        return;
                    //    }
                    //} else {
                    //	console.log(methods._css(Variable[_key].options.target[0], "left"));
                    //	if(parseInt(methods._css(Variable[_key].options.target[0], "left")) < 10){
                    //		return;
                    //	}
                    //}

                    //if(disY > 0){
                    //	if (parseInt(methods._css(Variable[_key].options.target[0], "top")) + $(Variable[_key].options.target).outerHeight() + 10 > $(window).height()) {
                    //        return;
                    //    }

                    //}else{
                    //	if(parseInt(methods._css(Variable[_key].options.target[0], "top")) < 10){
                    //		return;
                    //	}
                    //}


                    Variable[_key].options.target[0].style.left = parseInt(Variable[_key].options.left) + disX + "px";
                    Variable[_key].options.target[0].style.top = parseInt(Variable[_key].options.top) + disY + "px";
                    if (event.preventDefault) {
                        event.preventDefault();
                    }
                    return false;
                }

                if (typeof callback == "function") {
                    /*                    callback(parseInt(Variable[_key].options.params.left) + disX, parseInt(Variable[_key].options.params.top) + disY);*/
                }
            }
        }
    };


    var methods = {
        init: function(options) {
            var setting = {
                trigger: "",
                target: ""
            };
            setting = $.extend(setting, options);
            setting.trigger = $(this);
            var key = "drag-widget-" + new Date().getTime();
            $(this).attr("drag-widget-key", key);
            Variable[key] = new Drag(this.selector, setting);


        },
        _css: function(o, key) {
            return o.currentStyle ? o.currentStyle[key] : document.defaultView.getComputedStyle(o, false)[key];
        }
    };

    $.fn.Drag = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method' + method + 'does not exist');
        }
    };
    window.Drag = Drag;
})(window, document, $);